package cn.shadow.server;

import cn.shadow.message.WorkerFactory;
import cn.shadow.config.ServerConfig;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.io.Closeable;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ConnectionServer implements Closeable, ApplicationListener<ContextRefreshedEvent> {
    private final NettyServer nettyServer;

    public ConnectionServer(WorkerFactory factory, ServerConfig serverConfig) {
        this.nettyServer = new NettyServer(serverConfig.getPort(), factory);
    }

    public void start() {
        nettyServer.start();
    }

    @Override
    public void close() {
        nettyServer.close();
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        start();
    }
}
